using Sandia.MVCFramework.Common;
using Sandia.MVCFramework.Data;
using Sandia.MVCFramework.Request;
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace Sandia.MVCFramework.ExportTools
{
    /// <summary>
    /// Abstract implementation of an IImporter for importing
    /// from a named file
    /// </summary>
    public abstract class AbstractFileImporter: IImporter
    {
        /// <summary>
        /// The publisher to publish the imported data to
        /// </summary>
        protected IPublisher publisher;

        /// <summary>
        /// Processes an import request
        /// </summary>
        /// <param name="filename">The filename to import from</param>
        protected abstract IData processRequest(string filename);

        /// <summary>
        /// Whether or not this item has been disposed
        /// </summary>
        private bool isDisposed = false;

        #region IRequestProcessor Members

        /// <summary>
        /// The IRequest types that this RequestProcessor would like to process.
        /// </summary>
        public IList<Type> DesiredRequests
        {
            get
            {
                List<Type> desiredRequests = new List<Type>();
                desiredRequests.Add(typeof(ImportRequest));
                return desiredRequests;
            }
        }

        /// <summary>
        /// Processes an incoming request
        /// </summary>
        /// <param name="request">The IRequest to process</param>
        public void ProcessRequest(IRequest request)
        {
            try
            {
                if (request is ImportRequest)
                {
                    ImportRequest importRequest = (ImportRequest)request;
                    IData result = this.processRequest((string)importRequest[ImportFilenameRequestParameter.NAME].Data);
                    if (result != null)
                    {
                        this.publisher.PublishData(new DataPacket(result, request.RequesterID));
                    }
                }
            }
            catch (Exception e)
            {
                // Generate a log entry
                LoggingTools.SendToLog(
                    "Unable to import\n " + e.Message,
                    LoggingCategories.ERROR,
                    100);
            }
        }

        /// <summary>
        /// Whether or not this item has been disposed
        /// </summary>
        public bool IsDisposed
        {
            get { return isDisposed; }
        }

        /// <summary>
        /// Called while disposing
        /// </summary>
        public event EventHandler Disposed;


        #endregion

        #region IDataPacketProducer Members

        /// <summary>
        /// Initialize the data packet producer by telling
        /// it who publishes the data packets produced
        /// </summary>
        /// <param name="publisher">The publisher for data
        /// packets produced</param>
        public void InitDataPacketProducer(IPublisher publisher)
        {
            this.publisher = publisher;
        }

        #endregion


        #region IDisposable Members

        /// <summary>
        /// Set IsDisposed to true and raise the disposed event
        /// </summary>
        public virtual void Dispose()
        {
            isDisposed = true;
            if (Disposed != null) Disposed(this, new EventArgs());
        }

        #endregion
    }
}
